<?php
###########################################################################
#  Copyright (C) 2006-2007 Glyphix, Inc. <briz@glyphix.com>               #
#                                                                         #
#  Permission is hereby granted, free of charge, to any person obtaining  #
#  a copy of this software and associated documentation files (the        #
#  "Software"), to deal in the Software without restriction, including    #
#  without limitation the rights to use, copy, modify, merge, publish,    #
#  distribute, sublicense, and/or sell copies of the Software, and to     #
#  permit persons to whom the Software is furnished to do so, subject to  #
#  the following conditions:                                              #
#                                                                         #
#  The above copyright notice and this permission notice shall be         #
#  included in all copies or substantial portions of the Software.        #
#                                                                         #
#  Except as contained in this notice, the name(s) of the above           #
#  copyright holders shall not be used in advertising or otherwise to     #
#  promote the sale, use or other dealings in this Software without       #
#  prior written authorization.                                           #
#                                                                         #
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        #
#  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     #
#  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. #
#  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR      #
#  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,  #
#  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR  #
#  OTHER DEALINGS IN THE SOFTWARE.                                        #
###########################################################################

/**
 * GXValidator
 *
 * A data validation class, providing the ability to deal with masks as well as valid value tables.
 * @author		Brad Brizendine <briz@glyphix.com>
 * @link		http://www.glyphix.com/
 * @license		http://opensource.org/licenses/bsd-license.php BSD License
 * @version		1.0
 * @package		GXPage
 * @subpackage	Modules
 * @copyright	Copyright 2006 - 2007, Glyphix, Inc.
 * @uses		GXModule
 *
 * @example
 * GXValidator::state('US','CA')	// returns true/false
 */

class GXValidator {

	// source
	public static $src = array(
		'region'	=> 'countries.xml'
		);

	// db cache vars
	public static $region;

	/**
	 * cache
	 *
	 * Loads and returns the specified validation database.
	 * @param string $what the database to load
	 * @return object GXDOM or false on failure
	 */
	private static function cache( $what = null ){
		// must have a database name
		if( !$what ){
			trigger_error( 'Missing what', E_USER_ERROR );
			return false;
		}
		// database name must have a source
		if( !self::$src[$what] ){
			trigger_error( 'Invalid validation database value (' .$what .')', E_USER_ERROR );
			return false;
		}
		// if specified class var is not an object, load it; note the two dollar signs
		if( !is_object(self::$$what) ){
			self::$$what = new GXDOM( dirname(__FILE__) .DIRECTORY_SEPARATOR  .'Data' .DIRECTORY_SEPARATOR .self::$src[$what] );
		}
		return self::$$what;
	}

	/**
	 * country
	 *
	 * Validates a country code.
	 * @param string $code
	 * @return boolean
	 */
	public static function country( $code = null ){
		if( !$code ){
			trigger_error( 'Missing code', E_USER_ERROR );
			return false;
		}
		$db = self::cache('region');
		// run the xpath, get the first result, and that result must be an object
		return is_object($db->exec->xpath( '/*/c[ @code = "' .$code .'" ]', 0 ));
	}

	/**
	 * state
	 *
	 * Validates a state code.
	 * @param string $code
	 * @return boolean
	 */
	public static function state( $countryCode = null, $code = null ){
		if( !$countryCode || !$code ){
			trigger_error( 'Missing countryCode or code', E_USER_ERROR );
			return false;
		}
		$db = self::cache('region');
		// run the xpath, get the first result, and that result must be an object
		return is_object($db->exec->xpath( '/*/c[ @id = "' .$countryCode .'" ]/sp[ @id = "' .$code .'" ]', 0 ));
	}

}

?>